<?php

namespace Xuan\Controller;

class GoodsController extends CommonController {

    public function lists() {

        $keyword = trim(I('get.keyword'));
        $cat_id = I('get.cat_id', 0, 'int');
        $shop_id = I('get.shop_id', 0, 'int');
        $sql = "1 = 1";
        if ($keyword != '') {
            $sql .= " AND name like '%" . $keyword . "%'";
        }
        if ($cat_id > 0) {
            $sql .= " AND cat_id = '" . $cat_id . "'";
        }
        if ($shop_id > 0) {
            $sql .= " AND sid = '" . $shop_id . "'";
        }
        $count = M('goods')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, C("pagenum"));
        $lists = M('goods')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id DESC')->select();
        foreach ($lists as $k => $v) {
            $lists[$k]['salenum'] = M('order_goods')->where("gid = " . $v['id'] . "")->count();
        }
        $cats = M('goods_cat')->where("is_check = 1")->select();
        $shops = M("shop")->order("ord ASC")->select();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keyword", $keyword);
        $this->assign("cat_id", $cat_id);
        $this->assign("shop_id", $shop_id);
        $this->assign("shops", $shops);
        $this->assign("cats", $cats);
        $this->display();
    }

    public function detail() {
        $id = I('get.id', '0', 'int');
        if ($id == 0) {
            $detail['is_check'] = 1;
        } else {
            $detail = M("goods")->where("id =" . $id . "")->find();
            $cats = M("goods_cat")->order("ord ASC")->where("sid = " . $detail['sid'] . "")->select();
        }
        $shops = M("shop")->order("ord ASC,id DESC")->select();
        $this->assign("detail", $detail);
        $this->assign("shops", $shops);
        $this->assign("cats", $cats);
        $this->assign("units", getDictionary('goods_unit', 0));
        $this->assign("attr", getDictionary('goods_attr', 0));
        $this->display();
    }

    public function detail_post() {
        $id = I('post.id', '', 'int');
        $names = I('post.name');
        $data['sid'] = I("post.shop_id");
        $data['unit'] = I("post.units");
        $data['price'] = I("post.price");
        if (I("post.discountCheckbox") == 'on') {
            $data['discount'] = I("post.discount");
        } else {
            $data['discount'] = 0;
        }
        $attr = I("post.attr");
        if ($attr) {
            $data['attr'] = implode(",", $attr);
        }
        $data['intro'] = I("post.intro");
        $data['ord'] = I("post.ord", 30, 'int');
        $data['is_check'] = I('post.is_check', '0', 'int');
        $data['cat_id'] = I("post.cat_id");
        $data['price'] = I("post.price");
        $path = C('goodsFile');
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = C('upload_max_size'); // 设置附件上传大小
        $upload->rootPath = $path; // 设置附件上传目录
        $upload->saveName = array('uniqid', '');
        $upload->autoSub = false;
        $info = $upload->upload();
        if ($info) {
            $uploadInfo = $info['logo'];
            if ($uploadInfo) {
                $type = strtolower(substr(strrchr($uploadInfo['name'], '.'), 1)); //文件类型
                $data['logo'] = $uploadInfo['savename']; //数据库保存名
                if (in_array($type, explode(",", C("picTypes")))) {
                    $image = new \Think\Image();
                    $image->open($path . $data['logo']);
                    $image->thumb(190, 139, \Think\Image::IMAGE_THUMB_FIXED)->save($path . "190_" . $data['logo']);
                }
            }
        }
        if ($id > 0) {
            $data['name'] = $names;
            M("goods")->where("id = " . $id . "")->save($data);
        } else {
            $names = array_unique(array_filter($names));
            foreach ($names as $k => $v) {
                if ($v) {
                    $data['name'] = $v;
                    $data['ord'] = I('post.ord', '30', 'int') + $k;
                    M("goods")->add($data);
                }
            }
        }
        if ($id > 0) {
            $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            $this->success('添加成功！', U('Goods/lists'));
        }
    }

    public function cat() {
        $shops = M("shop")->order("ord ASC")->select();
        $sql = "1=1";
        $keyword = trim(I('get.keyword'));
        $shop_id = I('get.shop_id', 0, 'int');
        if ($keyword) {
            $sql .= " AND name like '%" . $keyword . "%'";
        }
        if ($shop_id > 0) {
            $sql .= " AND sid = '" . $shop_id . "'";
        }
        $count = M('goods_cat')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, C("pagenum"));
        $lists = M('goods_cat')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('ord ASC,id desc')->select();
        foreach ($lists as $k => $v) {
            $lists[$k]['num'] = M('goods')->where("cat_id = " . $v['id'] . "")->count();
        }
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keyword", $keyword);
        $this->assign("shops", $shops);
        $this->assign("shop_id", $shop_id);
        $this->display();
    }

    public function cat_detail() {
        $shops = M("shop")->order("ord ASC")->select();
        $id = I('get.id', '', 'int');
        if ($id > 0) {
            $detail = M("goods_cat")->where("id =" . $id . "")->find();
        } else {
            $detail['is_check'] = 1;
        }
        $this->assign("shops", $shops);
        $this->assign("detail", $detail);
        $this->display();
    }

    public function cat_detail_post() {
        $id = I('post.id', '', 'int');
        $data['name'] = I('post.name');
        $data['sid'] = I("post.shops");
        $data['ord'] = I("post.ord", 30, 'int');
        $data['is_check'] = I('post.is_check', '0', 'int');
        if ($id > 0) {
            M("goods_cat")->where("id = " . $id . "")->save($data);
        } else {
            M("goods_cat")->add($data);
        }
        if ($id > 0) {
            $this->success('修改成功！', session('QUERY_STRING'));
        } else {
            $this->success('添加成功！', U('Goods/cat'));
        }
    }

    public function unit() {//商品单位
        $this->assign("lists", getDictionary('goods_unit', 0));
        $this->display();
    }

    public function unit_detail() {//商品单位详情
        $id = I("get.id", 0, 'int');
        $detail = getStaticInfo($id, "dictionary");
        $this->assign("detail", $detail);
        $this->display();
    }

    public function unit_detail_post() {
        $id = I("post.id", 0, 'int');
        $data['name'] = trim(I("post.name"));
        $data['ord'] = I("post.ord", 0, 'int');
        if ($id > 0) {
            M("dictionary")->where("id = " . $id . "")->save($data);
            $this->success("修改商品单位成功！", session('QUERY_STRING'));
        } else {
            $data['code'] = 'goods_unit';
            M("dictionary")->add($data);
            $this->success("添加商品单位成功！", U("Goods/unit"));
        }
        clearTempFile();
    }

    public function label() {//商品标签
        $this->assign("lists", getDictionary('goods_label', 0));
        $this->display();
    }

    public function label_detail() {//商品标签详情
        $id = I("get.id", 0, 'int');
        $this->assign("detail", getStaticInfo($id, "dictionary"));
        $this->display();
    }

    public function label_detail_post() {
        $id = I("post.id", 0, 'int');
        $data['name'] = trim(I("post.name"));
        $data['ord'] = I("post.ord", 0, 'int');
        $path = C('shopsFile');
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = C('upload_max_size'); // 设置附件上传大小
        $upload->rootPath = $path; // 设置附件上传目录
        $upload->saveName = array('uniqid', '');
        $upload->autoSub = false;
        $info = $upload->upload();
        if ($info) {
            $uploadInfo = $info['logo'];
            if ($uploadInfo) {
                $type = strtolower(substr(strrchr($uploadInfo['name'], '.'), 1)); //文件类型
                $data['logo'] = $uploadInfo['savename']; //数据库保存名
                if (in_array($type, explode(",", C("picTypes")))) {
                    $image = new \Think\Image();
                    $image->open($path . $data['logo']);
                    $image->thumb(15, 15, \Think\Image::IMAGE_THUMB_FIXED)->save($path . $data['logo']);
                }
            }
        }
        if ($id > 0) {
            M("dictionary")->where("id = " . $id . "")->save($data);
            $this->success("修改商品标签成功！", session('QUERY_STRING'));
        } else {
            $data['code'] = 'goods_label';
            M("dictionary")->add($data);
            $this->success("添加商品标签成功！", U("Goods/label"));
        }
        clearTempFile();
    }

}

?>
